// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt
/* eslint-disable */

frappe.query_reports["Price Comparison"] = {
	"filters": [
		{
			"fieldname": "billno",
			"fieldtype": "MultiSelectList",
			"label": __("选择询价单"),
			"options": "SRM Inquiry",
			"get_data": function(txt) {
				return frappe.db.get_link_options("SRM Inquiry", txt);
			},
			"get_query": () =>{
				return {
					filters: { "docstatus": 1 }
				}
			}
		},
		// {
		// 	"fieldname": "creation_from",
		// 	"fieldtype": "Date",
		// 	"label": "制单日期从",
		// 	"default": frappe.datetime.add_days(frappe.datetime.get_today(), -180),
		// 	"width": 0
		// },
		// {
		// 	"fieldname": "creation_to",
		// 	"fieldtype": "Date",
		// 	"label": "制单日期到",
		// 	"default": frappe.datetime.add_days(frappe.datetime.get_today(), 1),
		// 	"width": 0
		// },
	],


	onload: function(report) {
		const field = frappe.query_report.get_filter("billno");
		field && field.$input.height(40);
		field && field.$wrapper.height(40);

		report.page.add_inner_button(__("生成比价单"), function() {
			let data = report.data;
			frappe.db.get_list('SRM Price Compare',{filters:{'from_inquiry': data[0].inquiry_billno}, fields:['name'], as_list:true})
				.then( (r) => {
					// 该询价单已经生成过比价单
					if (r[0]) {
						frappe.set_route("Form", "SRM Price Compare", r[0][0]);
						frappe.msgprint("已经生成比价单")
					}
					// 该询价单没有生成过比价单
					else {
						// 纯JS的New Doc
						let new_doc = frappe.model.get_new_doc("SRM Price Compare");
						new_doc.date = frappe.datetime.get_today();
						new_doc.price_type = "物料比价";
						new_doc.to_erp_price = 1;
						new_doc.from_inquiry = data[0].inquiry_billno;
						// 纯JS的New Child Doc
						for (let d of data){
							let new_doc_sub = frappe.model.add_child(new_doc, "SRM Price Compare Quotation Sub", "material_subs");
							new_doc_sub.supplier_number = d.supplier_number;
							new_doc_sub.supplier_name = d.supplier_name;
							new_doc_sub.material_number = d.material_number;
							new_doc_sub.material_name = d.material_name;
							new_doc_sub.material_model = d.material_model;
							new_doc_sub.price = d.price;
							new_doc_sub.tax_rate = d.tax_rate;
							new_doc_sub.tax_price = d.tax_price;
							new_doc_sub.moq = d.moq;
							new_doc_sub.mpq = d.mpq;
							new_doc_sub.lead_time = d.lead_time;
							new_doc_sub.selected = d.selected;
							new_doc_sub.selected_reason = d.selected_reason;
							new_doc_sub.from_inquiry_material_sub = d.from_inquiry_material_sub;
						}
						frappe.set_route("Form", new_doc.doctype, new_doc.name);
					}
				})
		}).addClass('btn-primary');
	},

	"formatter": function(value, row, column, data, default_formatter) {
		
		value = default_formatter(value, row, column, data);
		let format_fields = ["selected"];
		// if (in_list(format_fields, column.fieldname) && data && data["workflow_state"]) {
		if (format_fields && data && data["selected"]) {
			
			var color_field = data["selected"];
			let color = undefined;	
			if (color_field == 1) {
				color = 'green';
			}
			if (color){
				value =`<div style='margin:0px;padding-left:5px;color:${color}!important;'>${value}</div>`            
			}
		}

		return value;
	},

};

// 以下函数是调用API或者PY方法进行操控
function price_selected(data) {
	frappe.prompt({
		label: 'Selected Reason',
		fieldname: 'selected_reason',
		fieldtype: 'Select',
		options: ['最低价', '备选']
	}, (values) => {
		frappe.db.set_value('SRM Quotation Material Sub', data, {'selected_reason': values.selected_reason, 'selected': 1})
		.then(
			function(r) {
				// 刷新报表不改变过滤条件
				if (r) {frappe.query_report.refresh()}
			}
		);
	})
}

frappe.ui.keys.add_shortcut({
    shortcut: 'ctrl+q',
    action: () => {
		// console.log($('.dt-cell--focus').find('div').find('div').text())
		// frappe.msgprint($('.dt-cell--focus').find('div').find('div').text())
		let quotation_sub_name = $('.dt-cell--focus').find('div').find('div').text()
		if (quotation_sub_name) {
			frappe.confirm('确定取消选中?',
				() => {
					frappe.db.set_value('SRM Quotation Material Sub', quotation_sub_name, {'selected_reason': "", 'selected': 0})
					.then(function(r) {
						if (r) {
							frappe.query_report.refresh();
						}
					})
				}, () => {
					// action to perform if No is selected
				})
		}
    },
    page: this.page,
    description: __('Get Item INFO'),
    ignore_inputs: true,
});